<template>
  <el-container style="justify-content: center">

    <div class="info-text" style="color: #3A71A8">
      活 动 列 表
      <el-button v-if="roles.toString()!=='student'" class="createButton" type="success" size="small" @click.native="goCreate">>>创建</el-button>
    </div>

    <el-main class="info-main" v-loading="loading">
      <el-table
        :data="list.filter(data => !search || data.title.toLowerCase().includes(search.toLowerCase()))"
        style="width: 100%" stripe :default-sort = "{prop: 'status', order: 'descending'}">

        <el-table-column style="height: 60px">

          <template slot="header" slot-scope="scope">
            <el-input style="width: 100%" v-model="search" size="mini" placeholder="搜索活动"/>
          </template>

          <el-table-column type="expand" label="详情" width="35px">
            <template slot-scope="props">
              <el-form label-position="left" inline class="demo-table-expand">
                <el-form-item label="活动时间：">
                  <span>{{ props.row.displayTime }}</span>
                </el-form-item>
                <el-form-item label="活动地点：">
                  <span>{{ props.row.platforms }}</span>
                </el-form-item>
              </el-form>
            </template>
          </el-table-column>

          <el-table-column label="ID" prop="id" align="center" width="40px">
          </el-table-column>

          <el-table-column label="活动名" prop="title" align="center" sortable>
          </el-table-column>

        </el-table-column>

        <el-table-column label="活动状态" prop="status" align="center" width="120px" sortable>
          <template slot-scope="props">
            <el-button
              style="width: 80px"
              :disabled="props.row.status | statusDisableFilter"
              size="small"
              :type="props.row.status | statusStyleFilter"
              @click.native="handleApply(props.row.id)"
              >
              {{props.row.status | statusTextFilter}}
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-main>
  </el-container>

</template>

<script>
  import { mapGetters } from 'vuex'
  import { fetchList } from '@/api/article'

  export default {
    name: 'index',

    computed: {
      ...mapGetters([
        'authName',
        'roles',
      ]),
    },

    filters: {
      statusTextFilter(status) {
        const statusMap = {
          published: '报 名',
          draft: '已报名',
          deleted: '已结束'
        }
        return statusMap[status]
      },
      statusDisableFilter(status) {
        const statusMap = {
          published: false,
          draft: true,
          deleted: true
        }
        return statusMap[status]
      },
      statusStyleFilter(status) {
        const statusMap = {
          published: 'primary',
          draft: 'warning',
          deleted: 'info'
        }
        return statusMap[status]
      }
    },

    data(){
      return{
          data: null,
          listQuery: {
            page: 1,
            limit: 20 // 默认10
          },
          list: [],
          search: '',
          loading: false
      }
    },

    created() {
      this.getList()
    },

    methods:{
      getList() {
        this.Loading = true
        fetchList(this.listQuery).then(response => {
          this.list = response.data.list
          // console.log(this.list)
          this.total = response.data.total
          this.Loading = false
        })
      },
      handleApply(id){
        console.log(this.authName+"报名了: "+id)
      },
      goCreate(){
        this.$router.push({ name: 'MobileCreateActivity' })
      }
    }
  }
</script>

<style lang="scss">
  @import "../_styles/mobile-main";

  .el-input--mini .el-input__inner{
    height: 40px;
    margin-top: 9px;
    font-size: medium;
  }
  .el-table td, .el-table th{
    padding: 4px 0;
  }
  .createButton{
    position: absolute;
    top: -3px;
    right: 5px;
  }
</style>
